package cn.wonderland.resthome.controller;

import cn.wonderland.resthome.service.IOldmanService;
import cn.wonderland.resthome.domain.Oldman;
import cn.wonderland.resthome.query.OldmanQuery;
import cn.wonderland.basic.util.AjaxResult;
import cn.wonderland.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/oldman")
public class OldmanController {
    @Autowired
    public IOldmanService oldmanService;


    /**
     * 保存和修改公用的
     *
     * @param oldman 传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Oldman oldman) {
        if (oldman.getId() != null) {
            oldmanService.update(oldman);
        } else {
            oldmanService.add(oldman);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        oldmanService.delete(id);
        return AjaxResult.me().success();
    }

    //获取用户
    @GetMapping("/{id}")
    public Oldman get(@PathVariable("id") Long id) {
        return oldmanService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    public List<Oldman> list() {

        return oldmanService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    public PageList<Oldman> json(@RequestBody OldmanQuery query) {
        return oldmanService.queryByPage(query);
    }
}
